import { CITY_INIT, CITY_UPDATE, CITY_CLEAR } from "../actionTypes";
import { getLocalCity, getPoint } from "../../utils/baiduMap";

/**
 * 返回了 初始化城市的action 
 */
export const actionInitCity = () => {
  // return {
  //   type: CITY_INIT, value: {
  //     name: "北京"
  //   }
  // }
  return (dispatch) => {
    // 异步请求 axios 百度地图的获取当前城市也是异步
    getLocalCity()
      .then(res => {
        res.name = res.name.replace('市', '');
        dispatch({
          type: CITY_INIT,
          value: res
        })
      })
  }
}


/**
 * 传递城市名称来修改
 * @param {String} cityName 城市名称
 */
export const actionUpateCity = (cityName) => {
  return (dispatch) => {
    return  getPoint(cityName)
      .then(res => {
        const action = {
          type: CITY_UPDATE,
          value: {
            name: cityName,
            center: res
          }
        };
        dispatch(action);
      })
  }
}

/**
 * 清空当前城市
 */
export const actionClearCity = () => {
  return {
    type: CITY_CLEAR,
    value: {}
  }
}